#include "apue.h"

static void charatatime(char *);

int main() {
    pid_t pid;

    TELLWAIT();

    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid == 0) {
        WAIT_PARENT();
        charatatime("output from child\n");
    } else {
        charatatime("output from parent\n");
        TELL_CHILD(pid);
    }
    return 0;
}

static void charatatime(char *str) {
    char *ptr;
    int c;

    // 将标准输入输出设置为不带缓冲的，每一次字符输出都需要调用write
    setbuf(stdout, NULL);

    for (ptr = str; (c = *ptr++) != 0; ) {
        putc(c, stdout);
    }
}